package com.mttest.hotwords.comteoller;

import com.mttest.hotwords.entity.SearchLog;
import com.mttest.hotwords.redis.RedisKey;
import com.mttest.hotwords.service.SearchService;
import com.mttest.hotwords.vo.HotWordsVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sunqy
 * @description
 * @create 2020-08-31 22:21
 */
@Controller
public class SearchController {

    private static Logger LOGGER = LoggerFactory.getLogger(SearchController.class);

    @Resource
    private ZSetOperations zSetOperations;

    @Resource
    private SearchService searchService;

    /**
     * 搜索接口，采用es搜索，每次搜索增加关键字的score（搜索次数），搜索方法此处略
     * @param request
     * @return
     */
    @PostMapping("/search")
    public Object search(HttpServletRequest request){
        String word = request.getParameter("word");
        //插入搜索日志表
        LOGGER.info("插入搜索日志表，搜索关键词：{}",word);
        searchService.addSearchLog(SearchLog.newSearchLog(word));

        //模拟执行搜索
        Object object = new Object();

        return object;
    }


    /**
     * TODO 返回有序热词
     * @param request
     * @return
     */
    @GetMapping("/")
    public List<HotWordsVO> listHotWords(HttpServletRequest request){
        //从redis缓存取数据
                //取到则返回
                //取不到从数据库里面查询，返回，然后存进redis,从数据库里面取的话，去搜索日志表count，再存进热词表

        return new ArrayList<>();
    }


}
